﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.economy
{
    public class ImposeExe : ExeBase
    {
        public ImposeExe() { _name = ConfigStrings.S_Impose; _readable = ConfigStrings.SR_Impose; }
        override public long execute()
        {
            Dictionary<string, string> configs = getConfig();
            if (!configs.ContainsKey(ConfigStrings.enabled) || !configs[ConfigStrings.enabled].ToLower().Equals("true")) return next_day();

            MiscMgr mgr = ServiceFactory.getMiscManager();

            if (_user.isActivityRunning(ActivityType.ImposeEvent))
            {
                int event_result = mgr.getImposeEventInfo(_proto, _logger, _user);
                _user._impose_isImposeEvent = event_result == 0;
            }
            if (_user.isActivityRunning(ActivityType.RepayEvent))
            {
                int event_result = mgr.getRepayEventInfo(_proto, _logger, _user);
                _user._impose_isRepayEvent = event_result == 0;
            }

            if (_user._impose_isImposeEvent || _user._impose_isRepayEvent)
            {
                if (_user._impose_isImposeEvent)
                {
                    // 先看能否拿奖励
                    string[] rewards_strs = _user._impose_imposeEvent_rewardStatus.Split(',');

                    for (int i = 0; i < rewards_strs.Length; i++)
                    {
                        if (rewards_strs[i] == null || rewards_strs[i].Length == 0) continue;
                        if (rewards_strs[i].Equals("0")) continue; // 已经领取过
                        if (rewards_strs[i].Equals("1"))
                        {
                            mgr.getImposeEventReward(_proto, _logger, i);
                        }
                    }
                }

                if (_user._impose_isRepayEvent)
                {
                    // 先看能否拿奖励
                    string[] rewards_strs = _user._impose_repayEvent_rewardStatus.Split(',');

                    for (int i = 0; i < rewards_strs.Length; i++)
                    {
                        if (rewards_strs[i] == null || rewards_strs[i].Length == 0) continue;
                        if (rewards_strs[i].Equals("0")) continue; // 已经领取过
                        if (rewards_strs[i].Equals("1"))
                        {
                            mgr.getRepayEventReward(_proto, _logger, i);
                        }
                    }
                }
                int min_loyalty = 90;
                if (configs.ContainsKey(ConfigStrings.impose_loyalty)) Int32.TryParse(configs[ConfigStrings.impose_loyalty], out min_loyalty);
                if ((_user._impose_isImposeEvent && _user._impose_imposeEvent_numNow < _user._impose_imposeEvent_numMax) ||
                    (_user._impose_isRepayEvent && _user._impose_repayEvent_numNow < _user._impose_repayEvent_numMax))
                {
                    mgr.getImposeInfo(_proto, _logger, _user, min_loyalty);
                    if (_user._impose_count > 0)
                    {
                        if (_user._impose_hasCd && _user._impose_cdtime > 0) return _user._impose_cdtime;
                        else if (mgr.impose(_proto, _logger, _user, false, min_loyalty)) return immediate();
                        else return next_halfhour();
                    }
                    else
                    {
                        int gold_available = getGoldAvailable();
                        int force_impose_gold = 0;
                        if (configs.ContainsKey(ConfigStrings.force_impose_gold)) Int32.TryParse(configs[ConfigStrings.force_impose_gold], out force_impose_gold);

                        if (_user._impose_force_gold <= force_impose_gold && _user._impose_force_gold <= gold_available)
                        {
                            if (mgr.impose(_proto, _logger, _user, true, min_loyalty)) return immediate();
                            else return next_halfhour();
                        }
                        else
                        {
                            return next_day();
                        }
                    }
                }
                else return doNormalImpose(mgr);
            }
            else return doNormalImpose(mgr);
        }
        private long doNormalImpose(MiscMgr mgr)
        {
            Dictionary<string, string> configs = getConfig();
            int gold_available = getGoldAvailable();
            
            int force_impose_gold = 0, impose_reserve = 35, min_loyalty = 90;
            if (configs.ContainsKey(ConfigStrings.impose_reserve)) Int32.TryParse(configs[ConfigStrings.impose_reserve], out impose_reserve);
            if (configs.ContainsKey(ConfigStrings.force_impose_gold)) Int32.TryParse(configs[ConfigStrings.force_impose_gold], out force_impose_gold);
            if (configs.ContainsKey(ConfigStrings.impose_loyalty)) Int32.TryParse(configs[ConfigStrings.impose_loyalty], out min_loyalty);
            mgr.getImposeInfo(_proto, _logger, _user, min_loyalty);

            if (_user._impose_count <= impose_reserve && (_user._impose_force_gold > force_impose_gold || _user._impose_force_gold > gold_available))
                return next_day();
            // if cd > 30minutes, ignore
            if (_user._impose_hasCd && _user._impose_cdtime > 0) return _user._impose_cdtime;
            // impose
            if (_user._impose_count > impose_reserve)
            {
                if (mgr.impose(_proto, _logger, _user, false, min_loyalty)) return immediate();
                else return next_halfhour();
            }
            // force-impose
            if (_user._impose_force_gold <= force_impose_gold && _user._impose_force_gold <= gold_available)
            {
                if (mgr.impose(_proto, _logger, _user, true, min_loyalty)) return immediate();
                else return next_halfhour();
            }
            return next_day();
        }
    }
}
